    //
//  LocationViewController.m
//  SpendFunk
//
//  Created by bm on 4/30/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LocationViewController.h"
#import "image.h"
#import "SpendFunkAppDelegate.h"
#import "DisplayMap.h"
@implementation LocationViewController
@synthesize mapView;
-(id)init{
	m_pArrContent = [[NSMutableArray alloc] initWithObjects:@"View High Roller",@"View Purchases", nil];
	
	return self;
}

-(void)loadView{
	[super loadView];
	[self initView];
}

-(void)initView{
	NSString *path = nil;
	UIImage *img = nil;
	
	////
	path = [[NSString alloc] initWithFormat:@"%@",IMG_HEADER];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, -22, img.size.width, img.size.height)];
	[imgvBar setImage:img];
	[self.view addSubview:imgvBar];
	[imgvBar setUserInteractionEnabled:YES];
	[imgvBar release];
	//BKGR
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_VIEW];
	img = [UIImage imageNamed:path];
	UIImageView *imgvBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
	[imgvBkgr setImage:img];
	[self.view addSubview:imgvBkgr];
	[imgvBkgr setUserInteractionEnabled:YES];
	[imgvBkgr release];
	
	//bkgr user
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_LOCATION];
	img = [UIImage imageNamed:path];	
	UIImageView *imgvBkgrUser = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
	[imgvBkgrUser setImage:img];
	[imgvBkgrUser setUserInteractionEnabled:YES];
	[self.view addSubview:imgvBkgrUser];
	[imgvBkgrUser release];
	
	//BACK
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_BACK];
	img = [UIImage imageNamed:path];	
	m_pBtnBack = [[UIButton alloc] initWithFrame:CGRectMake(5, -14, img.size.width, img.size.height)];
	[m_pBtnBack setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnBack setBackgroundColor:[UIColor clearColor]];
	[m_pBtnBack setTitle:@"Back" forState:UIControlStateNormal];
	m_pBtnBack.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnBack addTarget:nil action:@selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnBack];
	
	// mkmapview
	mapView = [[MKMapView alloc] initWithFrame:CGRectMake(15, 90, 290, 120)];
	[mapView setMapType:MKMapTypeStandard];
	//[mapView setZoomEnabled:YES];
	[mapView setScrollEnabled:YES];
	[mapView setDelegate:self];
	[self.view addSubview:mapView];
	[mapView release];
	//label text
	int nX=0,nY=0,nW=0,nH=0;
	//label item
	nX=10;nY=28;nW=200;nH=30;
	m_pLblMoney = [[UILabel alloc] initWithFrame:CGRectMake(nX, nY, nW, nH)];
	[m_pLblMoney setBackgroundColor:[UIColor clearColor]];
	[m_pLblMoney setTextColor:[UIColor blueColor]];
	[m_pLblMoney setText:@"$26.00"];
	[m_pLblMoney setFont:[UIFont fontWithName:@"Arial" size:20]];
	[self.view addSubview:m_pLblMoney];
	[m_pLblMoney release];
	
	m_pLblLocationName = [[UILabel alloc] initWithFrame:CGRectMake(nX, nY+15, nW, nH+15)];
	[m_pLblLocationName setBackgroundColor:[UIColor clearColor]];
	[m_pLblLocationName setTextColor:[UIColor blueColor]];
	[m_pLblLocationName setText:@"NTUC Bukit"];
	[m_pLblLocationName setFont:[UIFont fontWithName:@"Arial" size:14]];
	[self.view addSubview:m_pLblLocationName];
	[m_pLblLocationName release];
	
	//table
	int nHRow = 40;
	m_pMainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 220, 320, 2*nHRow) style:UITableViewStylePlain];
	m_pMainTableView.delegate = self;
	m_pMainTableView.dataSource = self;
	m_pMainTableView.rowHeight = nHRow;
	[m_pMainTableView setShowsVerticalScrollIndicator:NO];
	[m_pMainTableView setBackgroundColor:[UIColor clearColor]];
	[self.view addSubview:m_pMainTableView];
	[m_pMainTableView reloadData];
	m_pMainTableView.scrollEnabled = YES;
}
// set 
-(void)setFS_ID:(NSString*)fsquareID gLat:(NSString*)gLat gLong:(NSString*)gLong aMount:(NSString*)amount Address:(NSString*)Address City:(NSString*)city{
	if (m_pFourSquareID) {
		[m_pFourSquareID release];
		m_pFourSquareID = nil;
	}
	m_pFourSquareID = [fsquareID retain];
	//
	double geoLat = [gLat doubleValue]; 
	double geoLong = [gLong doubleValue]; 
	// for mapview
	[self setMapView:geoLat Glong:geoLong Amount:amount Address:Address City:city];

	//
	m_pLblMoney.text = [amount retain];
	m_pLblLocationName.text = [NSString stringWithFormat:@"%@-%@",Address,city];
	[self.view	 setNeedsDisplay];

}
- (void)setMapView:(double)glat Glong:(double)glong Amount:(NSString*)amount Address:(NSString*)Address City:(NSString*)city{

	MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
	region.center.latitude = glat;//22.569722 ;
	region.center.longitude = glong;//88.369722;
	region.span.longitudeDelta = 0.01f;
	region.span.latitudeDelta = 0.01f;
	[self.mapView setRegion:region animated:YES]; 
	DisplayMap *ann = [[DisplayMap alloc] init]; 
	ann.title = Address;
	ann.subtitle = city; 
	ann.coordinate = region.center; 
	[self.mapView addAnnotation:ann];
	//[self initView];
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
	MKPinAnnotationView *pinView = nil; 
	if(annotation != mapView.userLocation) 
	{
		static NSString *defaultPinID = @"com.invasivecode.pin";
		pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
		if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
										  initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
		
		pinView.pinColor = MKPinAnnotationColorRed; 
		pinView.canShowCallout = YES;
		pinView.animatesDrop = YES;
	} 
	else {
		[mapView.userLocation setTitle:@"I am here"];
	}
	return pinView;
}

-(void)onClickBack{
	[g_AppDelegate openFeedView];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [m_pArrContent count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
	UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",IMG_BTN_ARROW]]];
	cell.accessoryView = imageView;
	cell.textLabel.text = [m_pArrContent objectAtIndex:indexPath.row];
	
	return cell;
}



#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	if(indexPath.row == 0){//view high roller
		[g_AppDelegate openHighView];
	}
	if(indexPath.row == 1){//view perchase
		[g_AppDelegate openFeedView];
	}
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
